// pages/address/index.js
import tips from '../../../class/Utils/Tips'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    allprosta: "success",
    staallpro: "unstaallpro"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {},
  onShow: function (options) {


    //获取高度宽度等
    var that = this;

    var cart = wx.getStorageSync('cart')
    console.log(cart);
    var len = cart.length;
    that.setData({
      show_count: len
    })

    wx.getSystemInfo({
      success: function (res) {
        var pageWidth = res.windowWidth - 20;
        var pjvieww = pageWidth * 0.25
        that.setData({
          scrollHeight: res.windowHeight,
          pjuserpicliH: pjvieww
        });
      }
    });

    var cart = wx.getStorageSync('cart')
    if (cart) {
      that.setData({
        carts: cart
      });
    } else {
      that.setData({
        carts: []
      });
    }
    var cart_count = wx.getStorageSync('cart_count')
    if (cart_count) {
      that.setData({
        cart_count: cart_count
      });
    } else {
      that.setData({
        cart_count: 0
      });
    }
    var cart_price = wx.getStorageSync('cart_price')
    if (cart_price) {
      that.setData({
        cart_price: cart_price
      });
    } else {
      that.setData({
        cart_price: 0.00
      });
    }

    this.cartcount();

  },

  /* 点击减号 */
  bindMinus: function (e) {
    var that = this;
    var child = e.currentTarget.dataset.child;
    var num = that.data.carts[0].pro[child].gg_num;
    var carts = that.data.carts;
    console.log(num)
    // 如果大于1时，才可以减
    if (num <= 1) {
      return false;
    }
    num = num - 1;
    carts[0].pro[child].gg_num = num;

    this.setData({
      carts: carts
    });
    wx.setStorageSync('cart', carts);
    this.cartcount();

  },
 
  /* 点击加号 */
  bindPlus: function (e) {
    var that = this;
    var child = e.currentTarget.dataset.child;
    var num = that.data.carts[0].pro[child].gg_num;
    var carts = that.data.carts;
    num = parseInt(num) + 1
    if (num > that.data.carts[0].pro[child].maxcount) {
      wx.showModal({
        title: '温馨提示',
        showCancel: false,
        content: '数量不能大于当前库存'
      })
      return false
    }
    carts[0].pro[child].gg_num = num;

    this.setData({
      carts: carts
    });
    wx.setStorageSync('cart', carts);
    this.cartcount();
  },


  // //增减 input
  // bindManual1: function (e) {

  //   var that = this;
  //   var child = e.currentTarget.dataset.child;
  //   var znum = that.data.pro[child].ggz_num;
  //   var thisnum = that.data.pro[child].gg_num;
  //   var num = e.detail.value;
  //   var carts = that.data.pro;

  //   if (num < 1 || num == "" || isNaN(num)) {
  //     wx.showModal({
  //       title: '温馨提示',
  //       showCancel: false,
  //       content: '至少买点吧'
  //     })
  //     num = thisnum
  //     console.log(num)
  //     carts[child].gg_num = num;
  //     that.setData({
  //       pro: carts
  //     })
  //     console.log(that.data.pro)
  //     return false
  //   }
  //   if (num > znum) {
  //     wx.showModal({
  //       title: '温馨提示',
  //       showCancel: false,
  //       content: '数量不能大于库存'
  //     })
  //     num = thisnum
  //     console.log(num)
  //     carts[child].gg_num = num;
  //     that.setData({
  //       pro: carts
  //     })
  //     console.log(that.data.pro)
  //     return false
  //   }

  //   carts[child].gg_num = num;

    
  //   that.setData({
  //     pro: carts
  //   });

  //   console.log(that.data.pro)
    

  // },

  bindManual: function (e) {

    var that = this;
    var child = e.currentTarget.dataset.child;
    var num = e.detail.value;

    var carts = that.data.carts;

    if (num < 1 || num == "" || isNaN(num)) {
      num = 1;
    }
    if (num > carts[0].pro[child].maxcount){
      num = carts[0].pro[child].maxcount;
    }

    carts[0].pro[child].gg_num = num;

    this.setData({
      carts: carts
    });
    wx.setStorageSync('cart', carts);
    this.cartcount();

  },
 
  
 
  del_cart: function (e) {
    var that = this
    wx.showModal({
      title: '温馨提示',
      content: '确定要删除购物中的商品吗?',
      success: function (res) {
        if (res.confirm) {
          var child = e.currentTarget.dataset.child;
          var proid = e.currentTarget.dataset.id;
          var carts = that.data.carts;


          carts[0].pro.splice(child, 1);
          var dp_len = carts[0].pro
          if (dp_len == 0) {
            carts.splice(0, 1);
          }
          that.setData({
            carts: carts
          });
          wx.setStorageSync('cart', carts);
         
         
          that.cartcount();

          
          var len = carts.length;
          that.setData({
            show_count: len
          })



        } else if (res.cancel) {

        }
      }
    })

  },

  dianji_ico: function (e) {
    var that = this
    var index = e.currentTarget.dataset.index;
    var cart = that.data.carts;
    cart[0].pro[index].select = "success";
    cart[0].pro[index].bind = "dianji_ico_circle";
    wx.setStorageSync('cart', cart);
    this.cartcount();
    that.setData({
      carts: cart
    })
  },

  dianji_ico_circle: function (e) {
    var that = this
    var index = e.currentTarget.dataset.index;

    var cart = that.data.carts;
    cart[0].pro[index].select = "circle";
    cart[0].pro[index].bind = "dianji_ico";
    wx.setStorageSync('cart', cart);
    this.cartcount();
    that.setData({
      carts: cart
    })

  },
  unstaallpro: function () {
    var that = this
    var cart = that.data.carts;
    for (var i = 0; i < cart[0].pro.length; i++) {
      cart[0].pro[i].select = "circle";
      cart[0].pro[i].bind = "dianji_ico";
    }
    wx.setStorageSync('cart', cart);
    this.cartcount();
    that.setData({
      carts: cart,
      allprosta: "circle",
      staallpro: "staallpro"
    })
  },
  staallpro: function () {
    var that = this
    var cart = that.data.carts;
    for (var i = 0; i < cart[0].pro.length; i++) {
      cart[0].pro[i].select = "success";
      cart[0].pro[i].bind = "dianji_ico_circle";
    }
    wx.setStorageSync('cart', cart);
    this.cartcount();
    that.setData({
      carts: cart,
      allprosta: "success",
      staallpro: "unstaallpro"
    })
  },


  //选择要付款的商品
  dianji_ico1: function (e) {
    var that = this
    
    var index = e.currentTarget.dataset.index;
    var cart = that.data.pro;
    var icos = cart[index].select
    var binds = cart[index].bind
    var thisprice = cart[index].gg_price
    var num = cart[index].gg_num
    var thiszongjia = thisprice * num
    console.log(thiszongjia)

    if (icos == 'success'){
      cart[index].select = 'circle'
      that.setData({
        pro: cart
      })
      that.cart_pricedata(thiszongjia, 0)
    }
    if (icos == 'circle') {
      cart[index].select = 'success'
      that.setData({
        pro: cart
      })
      that.cart_pricedata(thiszongjia, 1)
    }
    return

  },

  //增加商品价格
  cart_pricedata1: function (a,b){
    var that = this
    var presentprice = that.data.cart_price
    var aa = a
    var bb = b
    if(b == 1){
      console.log(a)
      that.setData({
        cart_price: presentprice + a
      })
    }
    if (b == 0) {
      console.log(a)
      that.setData({
        cart_price: presentprice - a
      })
    }
    
    
  },
  

  unstaallpro1:function(){
    var that = this
    var cart = that.data.pro;
    
  },
  staallpro1: function () {
    var that = this
    var icos = that.data.allprosta
    var cart = that.data.pro;
    
    if (icos == 'circle'){
      for (var i = 0; i < cart.length; i++) {
        cart[i].select = "success";
      }
      that.setData({
        pro: cart,
        allprosta: "success",
      })
    }else{
      for (var i = 0; i < cart.length; i++) {
        cart[i].select = "circle";
      }
      that.setData({
        pro: cart,
        allprosta: "circle",
      })
    }


    
  },
  gotoclassify:function(){

    wx.reLaunch({
      url: '/pages/shop/index/index'
    })
  },
  qujiesuan: function () {
    if (this.data.cart_count==0){
      wx.showToast({
        title: '请选择商品',
        image: '/image/icons/alert.png',
        mask: true,
        duration: 1500
      });
      return false
    }
  wx.navigateTo({
    url: '/pages/shop/order/tjorder/tjorder',
  })
  },
  cartcount: function () {
    var num = 0
    var select = 0
    var total_price = 0

    var yhq_price = 0

    var that = this;
    var cart = wx.getStorageSync('cart')
    var len = cart.length;
    if (len) {
      for (var i = 0; i < len; i++) {
        for (var j = 0; j < cart[i].pro.length; j++) {
          if (cart[i].pro[j].select == 'success') {
            total_price += parseInt(cart[i].pro[j].gg_num) * Number(cart[i].pro[j].gg_price)

           
            if (cart[i].pro[j].is_yhq == 1) {
              yhq_price += parseInt(cart[i].pro[j].gg_num) * Number(cart[i].pro[j].gg_price)
            }

            num += parseInt(cart[i].pro[j].gg_num);
            select++
          }
        }
      }

      var cart_len = cart[0].pro.length;
      if (cart_len == select) {
        that.setData({
          allprosta: "success"
        })
      } else {
        that.setData({
          allprosta: "circle"
        })
      }
    }


    wx.setStorageSync('cart_count', num);
    wx.setStorageSync('cart_price', total_price.toFixed(2));

    wx.setStorageSync('yhq_price', yhq_price.toFixed(2));

    that.setData({
      cart_count: num,
      cart_price: total_price.toFixed(2)
    })
  }
})